/*
 * Copyright (C) 2016 yixun All Rights Reserved.
 * 
 * SubscribeServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.basic.mapper.SubscribeMapper;
import com.emm.yixun.basic.service.SubscribeService;
import com.emm.yixun.common.model.Subscribe;
import com.emm.yixun.common.model.SubscribeInfo;
import com.emm.yixun.common.model.dto.SubscribeDto;
import com.emm.yixun.common.page.Page;

/**
 * [文件名称]<br>
 * SubscribeServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 客户认购表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2016-05-11 11:27:12 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("subscribeService")
public class SubscribeServiceImpl implements SubscribeService {

	@Autowired
	private SubscribeMapper mapper;

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<Subscribe> getPageModel(SubscribeDto entity, int begin, int end) {
		Integer total = mapper.count(entity);
		List<Subscribe> rows = mapper.list(entity, begin, end);
		Page<Subscribe> msr = new Page<Subscribe>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<Subscribe> selectListByDto(SubscribeDto entity) {
		return mapper.selectListByDto(entity);
	}

	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<Subscribe> selectListByDtoJ(SubscribeDto entity) {
		return mapper.selectListByDtoJ(entity);
	}

	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public Subscribe selectByDto(SubscribeDto entity) {
		return mapper.selectByDto(entity);
	}

	/**
	 * @param id
	 *            搜索条件
	 * @return 分页记录
	 */
	public Subscribe selectById(Long id) {
		if (id != null) {
			return mapper.selectById(id);
		}
		return null;
	}

	/**
	 * @param entity
	 *            新增记录的详细信息
	 * @return 新增行数
	 */
	public SubscribeDto create(SubscribeDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity
	 *            更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(SubscribeDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}

	/**
	 * @param entity
	 *            待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(SubscribeDto entity) {
		return mapper.del(entity);
	}

	/**
	 * @param entity
	 *            新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(SubscribeDto entity) {
		if (entity != null && entity.getId() != null) {
			return update(entity);
		} else {
			create(entity);
			return 1;
		}
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<SubscribeDto> getSubscribeList(SubscribeDto entity, int begin, int end) {
		Integer total = mapper.getSubscribeCount(entity);
		List<SubscribeDto> rows = mapper.getSubscribeList(entity, begin, end);
		Page<SubscribeDto> msr = new Page<SubscribeDto>(total, rows);
		return msr;
	}

	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public SubscribeDto getSubscribeDetail(SubscribeDto entity) {
		return mapper.getSubscribeDetail(entity);
	}

	public Page<SubscribeDto> getSubscribeDtoList(SubscribeDto entity, int begin, int end) {
		Integer total = mapper.getSubscribeDtoCount(entity);
		if(!StringUtils.isEmpty(entity.getIsExport())){ //不为空为导出
			begin = 0;
			end = total;
			entity.setOrders(null); //导出不按列表查询方式排序
		}
		List<SubscribeDto> rows = mapper.getSubscribeDtoList(entity, begin, end);
		Page<SubscribeDto> msr = new Page<SubscribeDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.basic.service.SubscribeService#selectSubscribeCustomer(java.lang.Long)
	 */
	public SubscribeDto selectSubscribeCustomer(Long id) {
		return mapper.selectSubscribeCustomer(id);
	}

	public List<Subscribe> findBusinessInfo(SubscribeDto subscribeDto) {
		return mapper.findBusinessInfo(subscribeDto);
	}
	
	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public SubscribeInfo totalNumberAndAmount(SubscribeDto entity) {
		return mapper.totalNumberAndAmount(entity);
	}
	
	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<Subscribe> createTabTypeSqlBySubscribe(SubscribeDto entity) {
		return mapper.createTabTypeSqlBySubscribe(entity);
	}
	
	/**
	 * 根据房间ID查询客户姓名和经纪人
	 * @param roomId
	 * @return 
	 */
	public SubscribeDto getCustomerByRoomId(Long roomId) {
		return mapper.getCustomerByRoomId(roomId);
	}
	
	/**
	 * 流程页面根据oldid查询认购信息
	 */
	public List<Subscribe> getBusinessDetailByOldId(SubscribeDto entity) {
		return mapper.getBusinessDetailByOldId(entity);
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public int getOverdueSignedListCount(SubscribeDto entity) {
		Integer total = mapper.getOverdueSignedListCount(entity);
		return total;
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<SubscribeDto> getOverdueSignedPageModel(SubscribeDto entity, int begin, int end) {
		Integer total = mapper.getOverdueSignedListCount(entity);
		List<SubscribeDto> rows = mapper.getOverdueSignedList(entity, begin, end);
		Page<SubscribeDto> msr = new Page<SubscribeDto>(total, rows);
		msr.setPageNumber(begin);
		msr.setPageSize(end);
		return msr;
	}

	public Page<SubscribeDto> getSubscribeListPageModel(SubscribeDto entity, int beginNum, int endNum) {
		Integer total = mapper.getSubscribeListCount(entity);
		List<SubscribeDto> rows = mapper.getSubscribeListPageModel(entity, beginNum, endNum);
		Page<SubscribeDto> msr = new Page<SubscribeDto>(total, rows);
		msr.setPageNumber(beginNum);
		msr.setPageSize(endNum);
		return msr;
	}

	public List<SubscribeDto> getSubscribeListByDto(SubscribeDto entity) {
		List<SubscribeDto> rows = mapper.getSubscribeListByDto(entity);
		return rows;
	}
}
